package com.samsonsoft.struts.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.Action;

public class ActorByIdAction implements Action{
	
	
	private final String userName = "root";
	private final String password = "";
	private final String driver = "com.mysql.jdbc.Driver";
	private final String url = "jdbc:mysql://localhost:3306/sakila";
	
	private int id;
	private String name;
	
	private static Log log = LogFactory.getLog("com.mobilefish.DemoAction");
	
	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}

	
	public int getId() {
		return id;
	}



	public void setId(int id) {
		this.id = id;
	}

	@Override
	public String execute() throws Exception {
		
		log.debug("This is my debug message.");
        log.info("This is my info message.");
        log.warn("This is my warn message.");
        log.error("This is my error message.");
        log.fatal("This is my fatal message.");
		
		
		  String ret = ERROR;
	      Connection conn = null;
	      System.out.println("executing ...");
	      
	      
	      

	      try {
	    	  System.out.println("try forName ...");
		         Class.forName(driver);
	    	  
	    	  System.out.println("try ...");
	         conn = DriverManager.getConnection(url, userName, password);
	         System.out.println("conn = " + conn);
	         String sql = "SELECT * FROM actor where actor_id= ?";
	         System.out.println(sql);
	         PreparedStatement ps = conn.prepareStatement(sql);
	         ps.setString(1, Integer.toString(id));
	         System.out.println(ps);
	         ResultSet rs = ps.executeQuery();
	         while (rs.next()) {
	            name = rs.getString(2) + " " + rs.getString(3);
	            setName(name);
	            System.out.println("Name = " + name);
	            ret = SUCCESS;
	         }
	      } catch (Exception e) {
	         ret = ERROR;
	      } finally {
	         if (conn != null) {
	            try {
	               conn.close();
	            } catch (Exception e) {
	            }
	         }
	      }
	      return ret;
	}

}
